const dbConfig = require("../config/dbConfig.js");
require("dotenv").config();
const Sequelize = require("sequelize");
const { DataTypes } = require("sequelize");

// 配置数据库连接
const sequelize = new Sequelize(
  process.env.DB_NAME,
  process.env.DB_USER,
  process.env.DB_PASS,
  {
    host: process.env.DB_HOST,
    dialect: dbConfig.dialect,
    operatorsAliases: 0,
    // logging: console.log, // 配置日志输出函数

    pool: {
      max: dbConfig.pool.max,
      min: dbConfig.pool.min,
      acquire: dbConfig.pool.acquire,
      idle: dbConfig.pool.idle,
    },
  }
);

// const sequelize = new Sequelize("pmoa", "root", "123456", {
//   dialect: "mysql", // 这里可以改成任意一种关系型数据库
//   host: "localhost", // 数据库服务器
//   timezone: "+08:00", // 这里是东八区，默认为0时区
// });

// 封装数据库连接和sequelize
const db = {};

db.DataTypes = DataTypes;
db.sequelize = sequelize;

//
db.user = require("./user.js")(sequelize, DataTypes);

db.project = require("./project.js")(sequelize, DataTypes);
db.user_project = require("./user_project.js")(sequelize, DataTypes);

// 解构
const needMoudle = ({ Need, changeFields } = require("./need.js")(
  sequelize,
  DataTypes
));
db.need = needMoudle.Need;
db.changedFields = needMoudle.changedFields;

db.user_need = require("./user_need.js")(sequelize, DataTypes);
db.needHistory = require("./needHistory.js")(sequelize, DataTypes);

db.bug = require("./bug.js")(sequelize, DataTypes);
db.user_bug = require("./user_bug.js")(sequelize, DataTypes);
db.bugHistory = require("./bugHistory.js")(sequelize, DataTypes);

// 用户-部门
// db.department = require("./department.js")(sequelize, DataTypes);
// db.user_department = require("./user_department.js")(sequelize, DataTypes);

module.exports = db;
